package com.joinway.framework.admin.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.framework.admin.bean.UserContext;
import com.joinway.framework.admin.bean.view.UserOverviewView;
import com.joinway.framework.admin.core.bean.view.MenuListView;
import com.joinway.framework.admin.core.bean.view.TreeMenuView;
import com.joinway.framework.admin.core.constant.AdminWebConstants;
import com.joinway.framework.admin.core.service.IAdminService;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.logging.annotation.LogMask;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.security.annotation.Security;
import com.joinway.framework.bean.security.type.CheckAction;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.core.plugin.PluginManager;
import com.joinway.framework.extension.utils.NumberUtils;
import com.joinway.framework.support.input.annotation.Render;
import com.joinway.framework.support.input.type.Case;

/**
 * @author Administrator
 *
 */
@Api(name = "管理后台通用接口", description = "")
@RestController
@RequestMapping("")
@Validated
@Security
public class AdminController extends ExceptionController {

	private final static Logger log = LoggerFactory.getLogger(AdminController.class);
	
	@Autowired protected List<IAdminService> services;
	
	@ApiMethod(description="管理员登录", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/login.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
//	@Login
	@Security(action=CheckAction.Login)
	@InputLog
	@OutputLog
	public View login(
			@Length(min=1,max=20) @RequestParam("name") @ApiQueryParam(name="name", description="用户名") @LogMask @Render(format=Case.Lower) String name
			, @Length(min=1,max=20) @RequestParam("password") @ApiQueryParam(name="password", description="密码") @LogIgnore String password
			, @LogIgnore HttpServletRequest request
	) throws Exception {
		getService().login(StringUtils.trimToEmpty(name).toLowerCase(), password, request);
		
//		request.getSession().setAttribute(AdminWebConstants.USER_CONTEXT_KEY, context);
		
		return new View();
	}
	
	@ApiMethod(description="获得用户可见菜单", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/navigator.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	public TreeMenuView navigator(@LogIgnore HttpServletRequest request) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		return getService().getNavigatorMenus(context.getUser().getId());
	}

	@ApiMethod(description="获得权限可见菜单", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/role/menu.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
//	@OutputLog(format=LogFormat.Json)
	public TreeMenuView roleMenu(
		@RequestParam(value="roleId",required=false,defaultValue="") 
		@ApiQueryParam(name="roleId",required=false,description="权限id,如果为空，返回所有菜单，否则返回权限可见的菜单") 
		String roleId) throws Exception {
		return getService().getRoleMenus(roleId);
	}

	@ApiMethod(description="获得用户所属应权限的可见菜单", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/user/role/menu.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public TreeMenuView userRoleMenu(@RequestParam(value="userId",required=false,defaultValue="") @ApiQueryParam(name="userId",description="用户id",required=false) String userId) throws Exception {
		return getService().getRoleList(NumberUtils.convertToInt(userId));
	}
	
	@ApiMethod(description="获得用户所属应权限的可见菜单列表", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/user/role/menu/list.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
//	@OutputLog
	public MenuListView userRoleMenuList(@ApiQueryParam(name="userId",description="用户id",required=false) String userId, @LogIgnore HttpServletRequest request) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		return getService().getUserRoleMenuList(context.getRoles());
	}

	@ApiMethod(description="获得登录用户信息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/user/overview.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public UserOverviewView userOverview(@RequestParam(value="userId") @Min(1) @ApiQueryParam(name="userId", description="用户id") int userId) throws Exception {
		return getService().getUserOverviewView(userId);
	}
	
	@ApiMethod(description="修改登录用户密码", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/user/password.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public View changePassword(
			@LogIgnore @RequestParam("oldPassword") @ApiQueryParam(name="oldPassword", description="旧密码") String oldPassword
			, @LogIgnore @RequestParam("newPassword") @ApiQueryParam(name="newPassword", description="新密码") String newPassword
			, @LogIgnore HttpServletRequest request
	) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		
		return getService().changePassword(context.getUser().getLoginName(), oldPassword, newPassword);
	}

	@ApiMethod(description="更新登录用户信息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/user/profile.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public View updateProfile(
			@LogIgnore @RequestParam(value="name") @ApiQueryParam(name="name", description="姓名") String name
			, @Render(format=Case.Lower) @LogIgnore @RequestParam("email") @ApiQueryParam(name="email", description="邮箱") String email
			, @LogIgnore HttpServletRequest request
	) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		return getService().updateProfile(context.getUser().getId(), name, email);
	}

	protected IAdminService getService() {
		return PluginManager.getInstance(services);
	}
	
}


